<?php
/**
 * --------------------------------------------------------------------
 * 会员
 * --------------------------------------------------------------------
 * @author Midnight, yangyunzhou@foxmail.com
 * --------------------------------------------------------------------
 * @copyright Midnight Studio
 * --------------------------------------------------------------------
 */
class MemberAction extends AdminBaseAction {

	/**
	 * --------------------------------------------
	 * 管理
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function manage() {
		$this->title = '会员管理';
		$this->get_data();
		$this->display('Admin:member/manage');
	}

	/**
	 * --------------------------------------------
	 * 获取数据集
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	private function get_data() {
		Import('org.util.page');
		$db = D('Member');
		$page = new Page($db->count(),$this->pageCount);
		$val = $db->field('id,name,email')->limit($page->firstRow . ',' . $page->listRows)->order('`id` DESC')->select();
		$this->page = $page->show();
		$this->list = $val;
	}

	/**
	 * --------------------------------------------
	 * 会员添加
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function add() {
		if($this->isFormPost() && $this->isValidToken()) {
			if(!$_POST['password']) unset($_POST['password']);
			else $_POST['password'] = md5($_POST['password']);
			/* 自动完成 */
			$_form_auto = array(array('password','md5',1,'function'),array('regdate','getTime',1,'function'));
			$db = D('Member');
			$db->setProperty('_auto',$_form_auto);
			if(!$db->create()) $this->error($db->getError());
			if($db->add()) {
				D('RoleUser')->set();
				$this->success('添加成功！');
			} else
				$this->error('添加失败！');
		} else {
			$this->title = '添加会员';
			$this->getSelect(D('Role')->get_role_list(),array('id','name'),0,'role_list');
			$this->display('Admin:member/add');
		}
	}

	/**
	 * --------------------------------------------
	 * 会员编辑
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function edit() {
		if($this->isFormPost() && $this->isValidToken()) {
			if(!$_POST['password']) unset($_POST['password']);
			else $_POST['password'] = md5($_POST['password']);
			$db = D('Member');
			if(!$db->create()) $this->error($db->getError());
			$db->{$db->pk} = $_GET['id'];
			if($db->save()) {
				D('RoleUser')->set($_POST['role'],$_GET['id'],'save');
				$this->success('编辑成功！');
			} else
				$this->success('没有任何更新！');
		} else {
			$this->title = '会员信息修改';
			$db = D('Member');
			$val = $db->where()->find($_GET['id']);
			$this->getSelect(D('Role')->get_role_list(),array('id','name'),$val['role'],'role_list');
			$this->val = $val;
			$this->display('Admin:member/edit');
		}
	}

	/**
	 * --------------------------------------------
	 * 会员删除
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function delete() {}

	/**
	 * --------------------------------------------
	 * 检索会员
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function search() {
		if($this->isFormPost() && $this->isValidToken()) {
			$this->title = '检索会员';
			dump($_POST);
			if(!$_POST['keyword']) $this->error("关键词不能为空！");
			Import('org.util.page');
			$db = D('Member');
			$count = $db->field('name')->where("name like '%{$_POST['keyword']}%' AND")->count();
			if($count == 0) $this->error('没有找到相关内容');
			/* 排序方式 */
			switch($_POST['order']){
				case 0: $order = 'id'; break;
				case 1: $order = 'regdate'; break;
				case 2:	$order = 'lastdate'; break;
			}
			/* 性别 */
			switch($_POST['sex']){
				//case -1 : $sex = ; break;
			}
			$page = new Page($count,$this->pageCount);
			$this->list = $db->field('id,name,email')->where("name='%{$_POST['keyword']}%'")->order("`{$order}` DESC")->select();
			$this->display('Admin:member/manage');
		} else {
			$this->title = '检索会员';
			$this->display('Admin:member/search');
		}
	}
}